package com.hzc.springchan.config.resthelp;

import com.hzc.springchan.bean.request.Request;
import com.hzc.springchan.constant.System;
import com.hzc.springchan.util.LogUtils;
import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * Rest Request请求体封装类
 * @author chen990627
 * @date 2022/10/14
 */
@Configuration
public class RequestConcurrent {

    @Value("128")
    private Integer maxRequestSize = 128;

    private Map<String, Request> container;

    {
        container = new HashMap<>(maxRequestSize);
    }

    public void log() {
        LogUtils.info(this, "requestConcurrent load complete");
    }

    public Request getCurrentRequest() {
        return container.get(MDC.get(System.MDC.TRANSACTION_ID));
    }

    public void putCurrentRequest(Request request) {
        container.put(MDC.get(System.MDC.TRANSACTION_ID), request);
    }

    public void removeCurrentRequest() {
        container.remove(MDC.get(System.MDC.TRANSACTION_ID));
    }
}
